package com.countryside.controller;

import com.countryside.pojo.*;
import com.countryside.pojo.query.EmployeeWorkQuery;
import com.countryside.service.EmployeeProjectService;
import com.countryside.service.EmployeeWorkService;
import com.countryside.service.RegionService;
import com.countryside.service.WorkCategoryService;
import com.countryside.service.impl.EmployeeWorkServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class EmployeeWorkController {

    @Autowired
    EmployeeWorkService employeeWorkService;
    @Autowired
    WorkCategoryService workCategoryService;
    @Autowired
    RegionService regionService;
    @Autowired
    EmployeeProjectService employeeProjectService;

    /**
     * 完善找活明信片
     * @param employeeWork
     * @param request
     * @param session
     * @return
     */
    @PostMapping("/addEmployeeWork")
    public String addEmployeeWork(EmployeeWork employeeWork, HttpServletRequest request,
                                  HttpSession session) {
        User user = (User) session.getAttribute("user");
        employeeWork.setUserId(user.getId());
        employeeWork.setUser(user);
        employeeWork.setAddress(request.getParameter("province") + request.getParameter("city"));
        employeeWorkService.addEmployeeWork(employeeWork);
        return "redirect:user/getEmployeeWork";
    }

    /**
     * 修改找活明信片
     * @param employeeWork
     * @param request
     * @param session
     * @return
     */
    @PostMapping("/updateEmployeeWork")
    public String updateEmployeeWork(EmployeeWork employeeWork, HttpServletRequest request,
                                     HttpSession session) {
        User user = (User) session.getAttribute("user");
        employeeWork.setUserId(user.getId());
        employeeWork.setAddress(request.getParameter("province") + request.getParameter("city"));
        employeeWorkService.updateEmployeeWork(employeeWork);
        return "redirect:user/getEmployeeWork";
    }

    /**
     * 查询所有工人信息
     * @param model
     * @param employeeWorkQuery
     * @return
     */
    @GetMapping("/queryAllEmployeeWork")
    public String queryAllEmployeeWork(Model model, EmployeeWorkQuery employeeWorkQuery) {
        PageInfo<EmployeeWork> employeeWorkPageInfo = employeeWorkService.listEmployeeWork(employeeWorkQuery);
        System.out.println(employeeWorkPageInfo);
        model.addAttribute("page", employeeWorkPageInfo);
        //点击全部分类
        List<WorkCategory> workCategories = workCategoryService.getCategory("00");
        model.addAttribute("workCategories", workCategories);
        //遍历出全国地区
        List<Region> regions = regionService.getRegion("000");
        model.addAttribute("regions", regions);
        return "gongren";
    }

    /**
     * 工人信息分类
     * @param id
     * @param employeeWorkQuery
     * @param model
     * @return
     */
    @GetMapping("/queryAllEmployeeWork/{id}")//1r1x
    public String getSonKind(@PathVariable("id") String id, //id为经过前端拼接而成
                             EmployeeWorkQuery employeeWorkQuery,
                             Model model) {
        //遍历出所有工种
        List<WorkCategory> workCategories = workCategoryService.getCategory("00");
        model.addAttribute("workCategories", workCategories);
        //遍历出所有省份
        List<Region> regions = regionService.getRegion("000");
        model.addAttribute("regions", regions);
        //初始化种类和地区
        WorkCategory category = null;
        Region region = null;
        //当拼接码中含有w时，相当于选中了一个工种
        if (id.contains("w")) {
            //获取该工种的编码
            String wId = id.substring(id.indexOf("w") - 2, id.indexOf("w"));
            //通过工种编号遍历出详细工种 并放到request域 wId作为详细工种的父标签id
            List<WorkCategory> sonWorkCategories = workCategoryService.getCategory(wId);
            model.addAttribute("sonWorkCategories", sonWorkCategories);
            //获取父标签的类别名称
            category = workCategoryService.getACategory(wId);
            model.addAttribute("category", category.getKind());
        }
        //当拼接码中含有p时，相当于选中一个省份
        if (id.contains("p")) {
            //获取省份编码
            String pId = id.substring(id.indexOf("p") - 3, id.indexOf("p"));
            //通过省份编码遍历出所有城市
            List<Region> sonRegions = regionService.getRegion(pId);
            model.addAttribute("sonRegions", sonRegions);
            //通过省份编码获取该省份，并且将该省份名称放到request域
            region = regionService.getARegion(pId);
            model.addAttribute("region", region.getPlace());
        }
        if (id.contains("s")) {
            String sId = id.substring(id.indexOf("s") - 2, id.indexOf("s"));
            //通过详细工种编码获取详细工种
            category = workCategoryService.getACategory(sId);
            model.addAttribute("category", category.getKind());
        }
        if (id.contains("c")) {
            String cId = id.substring(id.indexOf("c") - 3, id.indexOf("c"));
            //通过城市编码获取该城市
            region = regionService.getARegion(cId);
            model.addAttribute("region", region.getPlace());
        }
        if (category == null && region !=null) {
            //若只对地区进行了筛选，则遍历出该地区的招工信息
            employeeWorkQuery.setAddress(region.getPlace());
            PageInfo<EmployeeWork> employeeWorkPageInfo = employeeWorkService.listEmployeeWork(employeeWorkQuery);
            model.addAttribute("page", employeeWorkPageInfo);
        } else if (category != null && region == null) {
            //若只对职位进行了筛选，则遍历出该职位的招工信息
            employeeWorkQuery.setExpectedWork(category.getKind());
            PageInfo<EmployeeWork> employeeWorkPageInfo = employeeWorkService.listEmployeeWork(employeeWorkQuery);
            model.addAttribute("page", employeeWorkPageInfo);
        } else {
            //否则就通过进行职位和地区筛选
            employeeWorkQuery.setAddress(region.getPlace());
            employeeWorkQuery.setExpectedWork(category.getKind());
            PageInfo<EmployeeWork> employeeWorkPageInfo = employeeWorkService.listEmployeeWork(employeeWorkQuery);
            model.addAttribute("page", employeeWorkPageInfo);

        }
        return "gongren";
    }

    /**
     * 进入工人详细页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/toEmployee/{id}")
    public String toEmployee(@PathVariable("id")Integer id, Model model){
        EmployeeWork employeeWork = employeeWorkService.queryEmployeeByEmployeeId(id);
        model.addAttribute("work",employeeWork);
        String userId = employeeWork.getUserId();
        List<EmployeeProject> employeeProject = employeeProjectService.queryEmployeeProjectByUserId(userId);
        model.addAttribute("projects",employeeProject);
        List<EmployeeWork> employeeWorks = employeeWorkService.queryEmployeeWorkByAddress(employeeWork.getAddress().substring(0, 2),0,6);
        model.addAttribute("recommends",employeeWorks);
        return "worker";
    }


}
